package com.quan.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import com.quan.site.commons.vo.SiteAboutVo;
import com.quan.site.entity.SiteAbout;
import com.quan.site.mapper.SiteAboutMapper;
import com.quan.site.service.SiteAboutService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SiteAboutServiceImpl extends MyBaseServiceImpl<SiteAboutMapper, SiteAbout> implements SiteAboutService {

    @Autowired
    private SiteAboutMapper mapper;

    @Override
    public List<SiteAboutVo> queryList(PageUtils pageUtils, SiteAboutVo vo) {
        QueryWrapper<SiteAbout> queryWrapper = new QueryWrapper<SiteAbout>(vo);

        List<SiteAbout> list = super.list(pageUtils, queryWrapper);
        List<SiteAboutVo> collect = list.stream().map(item -> {
            SiteAboutVo bean = new SiteAboutVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, SiteAboutVo vo) {
        QueryWrapper<SiteAbout> queryWrapper = new QueryWrapper<SiteAbout>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<SiteAbout> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<SiteAbout> records = page.getRecords();

        final List<SiteAbout> collect = records.stream().map((item) -> {
            SiteAboutVo bean = new SiteAboutVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}